﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace NinjaToolbox.SubComponentModule
{
    public static class SubComponentEditorUtil
    {
        public readonly static GUIStyle headerGUIStyle;
        public readonly static GUIStyle headerCheckboxGUIStyle;
        public readonly static GUIStyle headerFoldoutGUIStyle;


        static SubComponentEditorUtil()
        {
            headerGUIStyle = new GUIStyle("ShurikenModuleTitle")
            {
                font = (new GUIStyle("Label")).font,
                border = new RectOffset(15, 7, 4, 4),
                fixedHeight = 22,
                contentOffset = new Vector2(20f, -2f)
            };

            headerCheckboxGUIStyle = new GUIStyle("ShurikenCheckMark");
            headerFoldoutGUIStyle = new GUIStyle("Foldout");
        }
        
        public static void DrawHeader(Rect rect, string title, bool isEnabled, out bool headerIsClicked, out bool enabledButtonClicked)
        {
            headerIsClicked = false;
            enabledButtonClicked = false;

            GUI.Box(rect, title, headerGUIStyle);

            var toggleRect = new Rect(rect.x + 4f, rect.y + 4f, 13f, 13f);
            var e = Event.current;

            if (e.type == EventType.Repaint)
                headerCheckboxGUIStyle.Draw(toggleRect, false, false, isEnabled, false);

            if (e.type == EventType.MouseDown)
            {
                var flag = false;

                if (toggleRect.Contains(e.mousePosition))
                {
                    enabledButtonClicked = !enabledButtonClicked;
                    flag = true;
                }

                else if (rect.Contains(e.mousePosition))
                {
                    headerIsClicked = !headerIsClicked;
                    flag = true;
                }

                if (flag)
                    e.Use();
            }
        }
    }
}
